package com.liutaw.nanjingair.views.activities;

import android.app.Application;
import android.os.Looper;
import android.widget.Toast;

import com.bugtags.library.Bugtags;
import com.bugtags.library.BugtagsOptions;
import com.liutaw.nanjingair.BuildConfig;
import com.liutaw.nanjingair.R;
import com.liutaw.nanjingair.dagger.component.ApplicationComponent;
import com.liutaw.nanjingair.dagger.component.DaggerApplicationComponent;
import com.liutaw.nanjingair.dagger.module.ApplicationModule;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

/**
 * Created by liutao on 16/3/15.
 */
public class AndroidApplication extends Application {
    private static AndroidApplication instance;

    public static AndroidApplication getInstance() {
        return AndroidApplication.instance;
    }

    private ApplicationComponent applicationComponent;

    @Override
    public void onCreate() {
        super.onCreate();
//        LeakCanary.install(this);
        initializeInjector();
        initializeLeakDection();
        initialImageLoader();
        AndroidApplication.instance = this;

        BugtagsOptions options = new BugtagsOptions.Builder().
                trackingLocation(true).//是否获取位置
                trackingCrashLog(true).//是否收集crash
                trackingConsoleLog(true).//是否收集console log
                trackingUserSteps(true).//是否收集用户操作步骤
                versionName("内测1.0").//自定义版本名称
                versionCode(1).//自定义版本号
                build();

        Bugtags.start("f8e9c04db61bf482391ffb079ecf60df", this, Bugtags.BTGInvocationEventBubble, options);
        Bugtags.setInvocationEvent(Bugtags.BTGInvocationEventShake);

    }

    private void initializeLeakDection() {
        if (BuildConfig.DEBUG) {
//            LeakCanary.install(this);
        }
    }


    private void initializeInjector() {
        this.applicationComponent = DaggerApplicationComponent.builder().applicationModule(new ApplicationModule(this)).build();
    }

    public ApplicationComponent getApplicationComponent() {
        return applicationComponent;
    }

    private void initialImageLoader() {
        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder().
                showImageForEmptyUri(R.drawable.ic_default_adimage)
                .cacheInMemory(true).cacheOnDisk(true).build();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                getApplicationContext()).defaultDisplayImageOptions(defaultOptions)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.LIFO).build();
        ImageLoader.getInstance().init(config);
    }


    public void showToastMessage(final String message) {
        new Thread() {
            @Override
            public void run() {
                Looper.prepare();
                Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
                Looper.loop();
            }
        }.start();

    }
}
